from django.db import models

from sportsground.utils.baseModels import BaseModels


class Msg(BaseModels):
    class Meta():
        db_table = 'user_msg'
        verbose_name = '消息'
        verbose_name_plural = verbose_name

    user = models.ForeignKey(to='User', on_delete=models.CASCADE, verbose_name="用户")
    message = models.CharField(default='', max_length=200, verbose_name="消息")
    read = models.BooleanField(default=False, verbose_name="是否已读")

    def Info(self):
        return {
            "id": self.id,
            "message": self.message,
            "read": self.read,
        }

    @classmethod
    def create(cls, user, message):
        msg = cls(user=user, message=message)
        msg.save()
        return msg

    @classmethod
    def Sign(cls, user, id):
        """ 标记已读 """
        msg = cls.objects.filter(user=user, id=id).first()
        if msg:
            msg.read = True
            msg.save()
